package com.aide.simplification.codelib.taglib;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.ecs.html.Input;
import org.apache.ecs.html.Option;
import org.apache.ecs.html.Select;

import com.aide.simplification.codelib.app.Codelib;
import com.aide.simplification.global.GlobalConfig;
import com.aide.simplification.global.Utils;

@SuppressWarnings("serial")
public class ComboxTag extends TagSupport {
	private String name = "";// 取text值所用名 （必填）
	private String id = "";// 取value值所有名 （必填）
	private String style = "";// 样式
	private boolean multiple = false;// 是否为多选列表
	private String attributes = "";// 验证代码
	private String defaultValue = "";// 要默认选中某项的值
	private String defaultSelectText = "";// 请选择项的文本
	private String defaultSelectValue = "";// 请选择项的值
	private String codeType = "";// 代码库类型 （必填）
	private String checkSub = "";// 是否验证子项 必选
	private String uuid = "0";
	private String show = "";

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			GlobalConfig gc = new GlobalConfig(Utils.getConfig("codelib.configfile"));
			Codelib codes = new Codelib();
			if (defaultSelectText.equals("")) {
				defaultSelectText = gc.getString("codes." + codeType
						+ ".defaultText");
				if (defaultSelectText == null || defaultSelectText.equals("")) {
					defaultSelectText = "请选择";
				}
			}
			if (defaultSelectValue.equals("")) {
				defaultSelectValue = gc.getString("codes." + codeType
						+ ".defaultValue");
				if (defaultSelectValue == null || defaultSelectValue.equals("")) {
					defaultSelectValue = "";
				}
			}
			if (defaultValue.equals("")) {
				defaultValue = defaultSelectValue;
			}
			if (checkSub.equals("")) {
				checkSub = gc.getString("codes." + codeType + ".checkSub");
				if (checkSub == null || checkSub.equals("")) {
					checkSub = "true";
				}
			} else if (!checkSub.equals("false")) {
				checkSub = "true";
			}

			int maxLv = gc.getInt("codes." + codeType + ".maxlv");
			int lv = 1;
			String flag = "_%";
			if (lv <= maxLv) {
				flag = gc.getString("codes." + codeType + "." + lv);
				if (flag == null) {
					flag = "_%";
				}
			}
			if (!show.equals("")) {
				Map<String, Object> map = codes
						.findById(codeType, defaultValue);
				String showname = "";
				if (map != null) {
					showname = map.get(codeType + "name").toString();
				}
				// __ ____
				// 11 1111 111111
				// 1111
				int currlv = 1;
				for (int i = 1; i <= maxLv; i++) {
					flag = gc.getString("codes." + codeType + "." + i);
					if (flag == null) {
						currlv = i;
						break;
					} else if (flag.lastIndexOf("_") + 1 == defaultValue
							.length()) {
						currlv = i;
						break;
					} else {
						currlv = i;
					}
				}
				String mid = defaultValue;
				for (int i = currlv - 1; i > 0; i--) {
					flag = gc.getString("codes." + codeType + "." + i);
					mid = mid.substring(0, flag.lastIndexOf("_") + 1)
							+ flag.substring(flag.lastIndexOf("_") + 1, flag
									.length());
					Map<String, Object> maptemp = codes.findById(codeType, mid);
					if (maptemp != null && !showname.equals("")) {
						showname = maptemp.get(codeType + "name").toString()
								+ show + showname;
					} else {
						showname = maptemp.get(codeType + "name").toString();
					}
				}
				try {
					out.println(showname);
				} catch (IOException e) {
				}
			} else {

				Input inputid = new Input();
				inputid.setType("hidden");
				inputid.setName(id);
				inputid.setID(id);
				inputid.setValue(defaultSelectValue);
				Input inputname = new Input();
				inputname.setType("hidden");
				inputname.setName(name);
				inputname.setID(name);
				inputname.setValue("");

				Select select = new Select();
				if (!"".equals(style)) {
					select.setStyle(style);
				}
				select.setName(id + uuid + "0");
				select.setID(id + uuid + "0");
				select.setMultiple(multiple);

				if (!attributes.equals("")) {
					Pattern pattern = Pattern.compile("\\s+");
					String[] attribute = pattern.split(attributes);
					for (int i = 0; i < attribute.length; i++) {
						String[] temp = attribute[i].split("=");
						if (temp != null && temp.length == 2) {
							inputid.addAttribute(temp[0], temp[1]);
						} else if (temp.length == 1) {
							inputid.addAttribute(temp[0], "");
						}
					}
				}

				String sql = "select " + codeType + "id as id," + codeType
						+ "name as name ,* from " + codeType
						+ "code where sign=1 and " + codeType + "id like '"
						+ flag + "'";

				String idt = "";
				String idtall = defaultValue;

				if (!defaultValue.equals(defaultSelectValue) && maxLv > 1) {
					flag = gc.getString("codes." + codeType + "." + lv);
					idt = defaultValue.substring(0, flag.lastIndexOf("_") + 1);
					idtall = idt
							+ flag.substring(flag.lastIndexOf("_") + 1, flag
									.length());
				}
				List<Map<String, Object>> list = codes.find(sql);
				Option option1 = new Option(defaultSelectText,
						defaultSelectValue, defaultSelectText);
				select.addElement(option1);
				for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
						.hasNext();) {
					Map<String, Object> map = iterator.next();
					Option option = null;
					if ("clientsort".equals(codeType)) {
						option = new Option(map.get("name").toString(), map
								.get("id").toString()
								+ "_" + map.get("Auditingsign").toString(), map
								.get("name").toString());
					} else {
						option = new Option(map.get("name").toString(), map
								.get("id").toString(), map.get("name")
								.toString());
					}
					if (idtall.equals(map.get("id").toString())) {
						option.setSelected(true);
						inputname.setValue(map.get("name").toString());
						if ("clientsort".equals(codeType)) {
							inputid.setValue(map.get("id").toString() + "_"
									+ map.get("Auditingsign").toString());
						} else {
							inputid.setValue(map.get("id").toString());
						}
					}
					if (!map.get("id").toString().equals(defaultSelectValue)) {
						select.addElement(option);
					}
				}
				if (1 == maxLv) {
					select.setOnChange("setSelectValue(" + 0 + ",'" + name
							+ "','" + id + "','" + defaultSelectValue + "','"
							+ uuid + "');");
				} else {
					if ("clientsort".equals(codeType)) {
						select.setOnChange("getSubSelect(" + 0 + ",'" + name
								+ "','" + id + "','" + codeType + "','" + maxLv
								+ "'," + checkSub + ",'" + defaultSelectValue
								+ "','" + uuid
								+ "'),selectexamineperson(this);");
					} else {
						select.setOnChange("getSubSelect(" + 0 + ",'" + name
								+ "','" + id + "','" + codeType + "','" + maxLv
								+ "'," + checkSub + ",'" + defaultSelectValue
								+ "','" + uuid + "');");
					}
				}
				try {
					out.print(select.toString());

					for (int i = 1; i < maxLv; i++) {
						String mid = "";
						String midall = "";
						Select selecti = new Select();
						selecti.setName(id + uuid + i);
						selecti.setID(id + uuid + i);
						selecti.setMultiple(multiple);
						Option optionsub = new Option(defaultSelectText,
								defaultSelectValue, defaultSelectText);
						selecti.addElement(optionsub);
						String upflag = gc.getString("codes." + codeType + "."
								+ (i));
						flag = gc
								.getString("codes." + codeType + "." + (i + 1));
						if (flag == null) {
							flag = "";
						}
						// 如果有默认值
						if (!defaultValue.equals(defaultSelectValue)
								&& !defaultValue.equals(mid)) {
							mid = defaultValue.substring(0, upflag
									.lastIndexOf("_") + 1);

							if (i + 1 == maxLv) {
								midall = mid
										+ defaultValue.substring(upflag
												.lastIndexOf("_") + 1,
												defaultValue.length());
							} else {
								midall = defaultValue.substring(0, flag
										.lastIndexOf("_") + 1);
							}

							if (flag.equals("")) {
								flag = "_%";
							} else {
								flag = flag.substring(0, upflag
										.lastIndexOf("_") + 1);
							}
							String sqlsub = "select " + codeType + "id as id,"
									+ codeType + "name as name from "
									+ codeType + "code where sign=1 and "
									+ codeType + "id like '" + mid + flag
									+ "' and " + codeType + "id<>'" + mid + "'";
							List<Map<String, Object>> listsub = codes
									.find(sqlsub);
							if (listsub == null || listsub.size() == 0) {
								if (!"".equals(style)) {
									selecti.setStyle(style);
									selecti.setStyle("display:none;" + style);
								} else {
									selecti.setStyle("display:none;");
								}
							} else {
								if (!"".equals(style)) {
									selecti.setStyle(style);
								}
							}
							for (Iterator<Map<String, Object>> iterator = listsub
									.iterator(); iterator.hasNext();) {
								Map<String, Object> map = iterator.next();
								Option option = new Option(map.get("name")
										.toString(), map.get("id").toString(),
										map.get("name").toString());
								if (midall.equals(map.get("id").toString())) {
									option.setSelected(true);
									if (!"".equals(style)) {
										selecti.setStyle(style);
										selecti.setStyle("display:'';" + style);
									} else {
										selecti.setStyle("display:'';");
									}
									inputname.setValue(map.get("name")
											.toString());
									if ("clientsort".equals(codeType)) {
										inputid.setValue(map.get("id")
												.toString()
												+ "_"
												+ map.get("Auditingsign")
														.toString());
									} else {
										inputid.setValue(map.get("id")
												.toString());
									}

								}
								selecti.addElement(option);
							}
						} else {// 如果未给默认值
							if (!"".equals(style)) {
								selecti.setStyle("display:none;" + style);
							} else {
								selecti.setStyle("display:none;");
							}
						}
						if (i + 1 == maxLv) {
							selecti
									.setOnChange("setSelectValue(" + i + ",'"
											+ name + "','" + id + "','"
											+ defaultSelectValue + "','" + uuid
											+ "');");
						} else {

							if ("clientsort".equals(codeType)) {
								selecti.setOnChange("getSubSelect(" + i + ",'"
										+ name + "','" + id + "','" + codeType
										+ "','" + maxLv + "'," + checkSub
										+ ",'" + defaultSelectValue + "','"
										+ uuid
										+ "'),selectexamineperson(this);");
							} else {
								selecti.setOnChange("getSubSelect(" + i + ",'"
										+ name + "','" + id + "','" + codeType
										+ "','" + maxLv + "'," + checkSub
										+ ",'" + defaultSelectValue + "','"
										+ uuid + "');");
							}
						}
						out.print(selecti.toString());
					}
					out.print(inputid.toString() + inputname.toString());
				} catch (IOException e) {
				}
			}
		} catch (Exception e) {
		}
		return EVAL_PAGE;
	}

	public String getShow() {
		return show;
	}

	public void setShow(String show) {
		this.show = show;
	}

	public String getDefaultSelectValue() {
		return defaultSelectValue;
	}

	public void setDefaultSelectValue(String defaultSelectValue) {
		this.defaultSelectValue = defaultSelectValue;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getAttributes() {
		return attributes;
	}

	public void setAttributes(String attributes) {
		this.attributes = attributes.trim();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getCheckSub() {
		return checkSub;
	}

	public void setCheckSub(String checkSub) {
		this.checkSub = checkSub;
	}

	public String getCodeType() {
		return codeType;
	}

	public void setCodeType(String codeType) {
		this.codeType = codeType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isMultiple() {
		return multiple;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getDefaultSelectText() {
		return defaultSelectText;
	}

	public void setDefaultSelectText(String defaultSelectText) {
		this.defaultSelectText = defaultSelectText;
	}
}
